package com.goldplusgold.tradedelay.app.home.live;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aliyun.vodplayerview.utils.ScreenUtils;
import com.aliyun.vodplayerview.widget.AliyunScreenMode;
import com.goldplusgold.live.LVDataManager;
import com.goldplusgold.live.bean.LDetailBean;
import com.goldplusgold.live.bean.VDetailBean;
import com.goldplusgold.live.channel.LiveBootstrap;
import com.goldplusgold.network.lib.http.netty.ChannelBootstrapManager;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.home.live.base.LiveBaseCallBack;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2017/7/13.
 */

public class LiveDetailActivity extends AppCompatActivity {

    public static class LiveDetailType{
        public static final int LIVE = 1;
        public static final int VIDEO = 2;
        public static final int TEXT_LIVE = 3;
        public static final int VIDEO_LIVE = 4;
    }

    private String liveID;
    private String roomID;
    protected Unbinder unbinder;
    private int type = LiveDetailType.LIVE;
    @BindView(R.id.live_sufaceview_back)
    TextView iv_back;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    @BindView(R.id.tabs)
    TabLayout tabLayout;
    @BindView(R.id.live_detail_layout_sufaceview)
    RelativeLayout live_detail_layout_sufaceview;
    @BindView(R.id.my_questions_bottom_layout)
    LinearLayout my_questions_bottom_layout;
    @BindView(R.id.live_item_layout_intro)
    RelativeLayout live_item_layout_intro;
    LiveDetailUpControler up;
    LiveDetailMiddleControler middle;
    LiveDetailDownControler detailDown;
    String uri;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(this.getIntent() != null){
            liveID = this.getIntent().getStringExtra("liveID");
            roomID = this.getIntent().getStringExtra("roomID");
            uri = this.getIntent().getStringExtra("uri");
            type = this.getIntent().getIntExtra("type",LiveDetailType.LIVE);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        MobclickAgent.setDebugMode(false);
        MobclickAgent.openActivityDurationTrack(false);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        setContentView(getLayoutId());
        unbinder = ButterKnife.bind(this);
        initData();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(up != null){
            up.stopPlay();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(up != null){
            up.play();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(up != null){
            up.onDestroy();
        }
    }

    private void initData(){
        if(liveID == null){
            return;
        }
        if(type == LiveDetailType.VIDEO){

            LVDataManager.getInstance().getVDetail(liveID, new Callback<VDetailBean>() {
                @Override
                public void onResponse(Call<VDetailBean> call, Response<VDetailBean> response) {
                    refreshVideoView(response.body());
                }

                @Override
                public void onFailure(Call<VDetailBean> call, Throwable t) {

                }
            });
        } else {
            LVDataManager.getInstance().getLDetail(liveID, new LiveBaseCallBack<LDetailBean>(this) {
                @Override
                public void onResponse(Call<LDetailBean> call, final Response<LDetailBean> response) {
                    super.onResponse(call , response);
                    refreshLiveView(response.body());
                }
                @Override
                public void onFailure(Call<LDetailBean> call, Throwable t) {
                    super.onFailure(call , t);
                }
            });
        }
    }

    private void refreshLiveView(LDetailBean lDetailBean){
        roomID = lDetailBean.getRoomID();
        if(type == LiveDetailType.TEXT_LIVE){
            iv_back.setBackgroundResource(R.drawable.white_arrow);
        }
        initView(lDetailBean.getLiveStatus());
        up.setData(lDetailBean.getLiveTitle(),lDetailBean.getLiveUrl(),lDetailBean.getBackgroundImg());
        middle.setData(lDetailBean);
    }

    private void initView(int liveStatu){
        if(up == null){
            up = new LiveDetailUpControler(this,live_detail_layout_sufaceview,type , liveID , liveStatu);
            middle = new LiveDetailMiddleControler(this,live_item_layout_intro,type);
            detailDown = new LiveDetailDownControler(this,tabLayout,viewPager,type,liveID,my_questions_bottom_layout,roomID , liveStatu);
        }
    }

    private void refreshVideoView(VDetailBean vDetailBean){
        initView(-1);
        up.setData(vDetailBean.getVideoTitle(),vDetailBean.getVideoUrl(),vDetailBean.getBackgroundImg());
        middle.setData(vDetailBean.getVideoTitle(),vDetailBean.getVideoTime());
        detailDown.setEssence(vDetailBean.getEssence());
    }

    public int getLayoutId() {
        return R.layout.live_detail_layout;
    }

    @OnClick({R.id.live_sufaceview_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.live_sufaceview_back:
                this.finish();
                break;
        }
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(up != null){
            up.onConfigurationChanged(newConfig);
        }
    }


}
